package com.hz.house.service.home;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hz.house.bean.entity.BuildingDianPingBean;
import com.hz.house.bean.vo.KaiPanDecoInfo;
import com.hz.house.cache.HomePageCacheService;
import com.hz.house.common.ResultUtil;
import com.hz.house.dao.BuildingDianPingMapper;

/**
 * 	楼盘点评相关业务
 * @author lfw
 */
@Service(value = "buildingDianPingService")
public class BuildingDianPingServiceImpl implements BuildingDianPingService{

	
	@Resource
	private HomePageCacheService homePageCacheService;//首页缓存管理
	
	@Autowired
	private BuildingDianPingMapper dpMapper;
	
	
	/**
	 * 	分页查询点评列表并缓存。
	 */
	@Override
	public Object getDianpingByPage(Integer page,String keyword) {
		if(page==null)
			page=1;
		if(page<=0)
			return ResultUtil.fail("页码异常");
		//走检索
		if(!StringUtils.isEmpty(keyword)) 
			return searchDianping(page,keyword);
		//走普通列表
		List<BuildingDianPingBean>  list = homePageCacheService.getDianPingList(page);
		if(list==null) {
			//走数据库
			list = dpMapper.getDianPingList((page-1)*10);
			if(list.size()>0) {
				List<KaiPanDecoInfo> dcs = this.dpMapper.getDecoInfo(list);
				for(BuildingDianPingBean dp:list) {
					for(KaiPanDecoInfo k:dcs) {
						if(k.getBuildingId().intValue()==dp.getBuildingId().intValue()) {
							dp.setDeco(k.getDeco());
							break;
						}
					}
				}
			}
			//格式化装修
			list.forEach(d->{
				d.setDeco(getDeco(d.getDeco()));
				if(d.getOverwrite()==null || d.getOverwrite()==1)
					d.setShowDeco(true);
				d.setOverwrite(null);
			});
			this.homePageCacheService.setDianpingListCache(page, list);
		}
		Integer total = this.homePageCacheService.getDianPingListTotal();
		if(total==null) {
			total = this.dpMapper.getDianPingTotal();
			this.homePageCacheService.setDianPingTotalCache(total);
		}
		Map<String,Object> m = new HashMap<>();
		m.put("rows", list);
		m.put("total", total);
		return ResultUtil.ok(m);
	}

	private String getDeco(String deco){
		if(deco==null)
			return "";
		if(deco.equals("rinsing"))
			return "清水";
		if(deco.equals("hardcover"))
			return "精装";
		return "精装/清水";
	}
	
	//走检索，后期重构为走全文es检索
	private Object searchDianping(Integer page,String keyword) {
		
		List<BuildingDianPingBean>  list = this.dpMapper.searchDianPingList((page-1)*10, keyword);
		if(list.size()>0) {
			//额外查询装修数据，太烦了
			List<KaiPanDecoInfo> dcs = this.dpMapper.getDecoInfo(list);
			for(BuildingDianPingBean dp:list) {
				for(KaiPanDecoInfo k:dcs) {
					if(k.getBuildingId().intValue()==dp.getBuildingId().intValue()) {
						dp.setDeco(k.getDeco());
						break;
					}
				}
			}
		}
		//格式化装修
		list.forEach(d->{
			d.setDeco(getDeco(d.getDeco()));
			if(d.getOverwrite()==null || d.getOverwrite()==1)
				d.setShowDeco(true);
			d.setOverwrite(null);
		});
		int total = this.dpMapper.searchDianPingListTotal(keyword);
		Map<String,Object> m = new HashMap<>();
		m.put("rows", list);
		m.put("total", total);
		return ResultUtil.ok(m);
	}
	
	public Object getLinkById(Integer id) {
		if(id==null)
			return ResultUtil.fail("错误");
		return ResultUtil.ok(null,this.dpMapper.getLinkByid(id));
	}
	
}
